package com.zb.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zb.entity.ZjFactoryAccountBalance;
import com.zb.entity.ZjFactoryOrderDeductionBill;
import com.zb.mapper.ZjFactoryOrderDeductionBillMapper;
import com.zb.service.ZjFactoryAccountBalanceService;
import com.zb.service.ZjFactoryOrderDeductionBillService;
import com.zb.service.ZjFactoryStrategyService;
import com.zb.utils.ZbOrderNoUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;

/**
 * <p>
 * 厂商工单扣款账单表，与工单一对一 服务实现类
 * </p>
 *
 * @author WEN
 * @since 2024-04-07
 */
@Slf4j
@Service
public class ZjFactoryOrderDeductionBillServiceImpl extends ServiceImpl<ZjFactoryOrderDeductionBillMapper, ZjFactoryOrderDeductionBill> implements ZjFactoryOrderDeductionBillService {
    @Resource
    private ZjFactoryOrderDeductionBillMapper factoryOrderDeductionBillMapper;
    @Resource
    private ZjFactoryAccountBalanceService factoryAccountBalanceService;
    @Resource
    private ZjFactoryStrategyService factoryStrategyService;

    // 保存厂商结单扣款信息
    @Override
    public void save(Long custAccId, Long factoryId, Long workId, String workNo, boolean allInclusiveFee, BigDecimal modulePrice) {
        if (null == factoryId || factoryId == 0) {
            return;
        }
        ZjFactoryAccountBalance accountBalance = factoryAccountBalanceService.getById(factoryId);
        ZjFactoryOrderDeductionBill bill = new ZjFactoryOrderDeductionBill();
        bill.setId(workId);
        bill.setCustAccId(custAccId);
        bill.setFactoryId(factoryId);
        bill.setWorkNo(workNo);
        bill.setCode(ZbOrderNoUtil.deductionNo(custAccId));
        if (allInclusiveFee) {
            bill.setOrderTotalPrice(modulePrice);
        } else {
            BigDecimal settlementPrice = factoryOrderDeductionBillMapper.getFactoryOrderSettlementPrice(workId);
            bill.setOrderTotalPrice(modulePrice.add(settlementPrice));
        }
        bill.setBillTime(new Date());
        bill.setDeductionTotalPrice(bill.getOrderTotalPrice());
        bill.setSettlementType(factoryStrategyService.getSettlementType(factoryId));
        if (accountBalance.getMoney().floatValue() >= bill.getOrderTotalPrice().floatValue()) {
            bill.setStatus(1);
            bill.setWaitPaymentTotalPrice(new BigDecimal("0.0"));
            bill.setDeductionTime(new Date());
            bill.setRemark("成功操作余额！");

            // 扣减账户余额
            ZjFactoryAccountBalance balance = new ZjFactoryAccountBalance();
            balance.setId(accountBalance.getId());
            balance.setMoney(accountBalance.getMoney().subtract(bill.getOrderTotalPrice()));
            balance.setFreezeMoney(accountBalance.getFreezeMoney().subtract(bill.getOrderTotalPrice()));
            factoryAccountBalanceService.updateById(balance);
        } else {
            bill.setStatus(2);
            bill.setWaitPaymentTotalPrice(bill.getOrderTotalPrice());
            bill.setRemark("余额不足，无法扣款，请充值！");
        }
        // 查询单据是否已存在
        if (null != this.getById(workId)) {
            this.updateById(bill);
        } else {
            this.save(bill);
        }
    }
}
